package com.kun.companion.controller;

import cn.hutool.core.util.StrUtil;
import com.kun.companion.core.AbstractController;
import com.kun.companion.core.context.annotation.InjectBean;
import com.kun.companion.core.domain.dto.StageSize;
import com.kun.companion.core.thread.ComThreadPool;
import com.kun.companion.core.ui.list.User;
import com.kun.companion.core.ui.view.CollapseListViewPane;
import com.kun.companion.core.util.ComUtil;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.*;
import javafx.scene.paint.Paint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author gzc
 */
public class HelloController extends AbstractController {
    private static final Logger logger = LoggerFactory.getLogger(HelloController.class);
    //    @FXML
    public CollapseListViewPane collapseListViewPane = new CollapseListViewPane();
    //    @FXML
    public BorderPane contentPane = new BorderPane();
    @FXML
    public HBox topBox;
    @FXML
    public HBox bottomBox;
    @FXML
    public TextField topField;
    @FXML
    public TextField bottomField;
    @InjectBean
    private ComThreadPool comThreadPool;


    @Override
    protected StageSize getMainStageSize() {
        return new StageSize(800, 1000,
                100, 100,
                ComUtil.DIMENSION.getWidth(), ComUtil.DIMENSION.getHeight());
    }

    //    @FXML
//    private KunWebViewPane kunWebViewPane;
//    private CollapseListView<String> collapseListView;

    @Override
    protected void initLayout() {
        collapseListViewPane.setPrefSize(200, 400);
        contentPane.setPrefSize(600, 400);
        collapseListViewPane.setContentPane(contentPane);
        contentPane.setBackground(new Background(new BackgroundFill(Paint.valueOf("#b49e9e"), null, null)));
        CollapseListViewPane.Collapse collapse1 = new CollapseListViewPane.Collapse("/app-icon.png", "按钮1");
        collapse1.setFunction((node) -> {
            if (node == null) {
//                node = StageSingletonContext.getInstance(TestController.class).getRootNode();
                BorderPane borderPane = new BorderPane();
                borderPane.setTop(new HBox());
                borderPane.setCenter(new HBox());
                borderPane.setBottom(new HBox());
                return borderPane;
            }
            return node;
        });
        CollapseListViewPane.Collapse collapse2 = new CollapseListViewPane.Collapse("/app-icon.png", "按钮2");
        collapse2.setFunction((node) -> {
            if (node == null) {
//                node = StageSingletonContext.getInstance(TestController.class).getRootNode();
                node = new Label("记录文件恶趣味可见其网络空间而且我看见额");
            }
            return node;
        });
        CollapseListViewPane.Collapse collapse3 = new CollapseListViewPane.Collapse("/app-icon.png", "按钮3");
        collapse3.setRunnable(() -> {
            ComUtil.uiThreadRun(() -> {
                contentPane.getChildren().setAll(new Label("按钮3"));
            });
        });
        collapseListViewPane.addItem(collapse1);
        collapseListViewPane.addItem(collapse2);
//        collapseListViewPane.addItem(collapse3);
//        FileTreeView fileTreeView = new FileTreeView(Path.of("e:\\"));
//        UserListView userListView = new UserListView();
        User user1 = new User("au1", "小明", true);
        User user2 = new User("au2", "小张", true);
        User user3 = new User("au3", "小往", false);
        User user4 = new User("au4", "小基", true);
//        userListView.getItems().addAll(user1, user2, user3, user4);

//        collapseListView = new CollapseListView<>();
//        collapseListView.setPrefSize(500,500);
//        collapseListView.itemsProperty().get().addAll("小明", "小猪", "小鸡");
//        // 自定义事件处理
        if (super.rootNode instanceof VBox vBox) {
//            vBox.getChildren().add(userListView);
//            vBox.getChildren().add(collapseListView);
        }
    }

    @FXML
    public void onbbb() {
        ComUtil.uiThreadRun(() -> {

            collapseListViewPane.collapse();
//            collapseListView.toggleCollapse();
//            collapseListView.refresh();
//            kunWebViewPane.loadPageAsync("https://www.elysium.pro/albums/photobook/100776");
//            kunWebViewPane.loadPageAsync("https://www.cnblogs.com/yangbl/p/16706292.html");
        });

//        System.out.println(comThreadPool);
//        CompletableFuture<Integer> supplyAsync = comThreadPool.supplyAsync(() -> 1);
//        System.out.println(supplyAsync.get());
//        Future<?> future = comThreadPool.submit(() -> {
//            System.out.println("su");
//        });
//        System.out.println(future.get());
//        Future<String> submit = comThreadPool.submit(() -> "call");
//        System.out.println(submit.get());
//        CompletableFuture<String> completableFuture = comThreadPool.supplyAsyncCallable(() -> "supplyAsync");
//        System.out.println(completableFuture.get());
    }

    @Override
    protected void reactiveWidth(double mainBoxWidth) {
//        progressBar.setPrefWidth(mainBoxWidth);
    }

    @Override
    protected void reactiveHeight(double mainBoxHeight) {
//        progressBar.setPrefHeight(20);

    }


    @FXML
    public void onTop() {
        String text = topField.getText();
        if (StrUtil.isBlank(text)) {
            return;
        }
        String value = "-fx-background-color: " + text;
        topBox.setStyle(value);
    }

    @FXML
    public void onBottom() {
        String text = bottomField.getText();
        if (StrUtil.isBlank(text)) {
            return;
        }
        String value = "-fx-background-color: " + text;
        bottomBox.setStyle(value);
    }
}